const fs = require('fs')
const path = require('path')
const ejs = require('ejs')
const utils = require('../utils')

// 通过懒惰函数的方式，确定样式后缀名
let fn = (tmp, dest, data, dir, file = 'style') => {
  let filetype = '.css'
  switch (data.csspre) {
    case 'sass': filetype = '.scss'
      break
    case 'less': filetype = '.less'
      break
    default: filetype = '.css'
  }
  fn = (tmp, dest, data, dir) => {
    if (!fs.existsSync(path.join(dest, dir))) {
      utils.mkdirsSync(path.join(dest, dir))
    }
    ejs.renderFile(path.join(tmp, dir, file + '.css'), data, (err, res) => {
      if (err) throw err
      fs.writeFileSync(path.join(dest, dir, `${file + filetype}`), res)
    })
  }

  return fn(tmp, dest, data, dir)
}

module.exports = fn